/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.results.comments;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import systole.domain.report.Report;
import systole.domain.report.template.Comment;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.comment.ControllerCommentEdition;
import systole.view.crud.comment.ControllerCommentView;
import systole.view.tabs.results.ResultDialogController;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class ControllerComment extends ResultDialogController {

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogComments dialogComments = (JDialogComments) this.form;
        dialogComments.getjCmbComments().removeAllItems();

        List<Comment> comments = ("F".equals(this.patient.getSex()))
                ? this.facadeDB.getCommentBroker().getCommentsForFemale()
                : this.facadeDB.getCommentBroker().getCommentsForMale();
        Iterator<Comment> it = comments.iterator();
        while (it.hasNext()) {
            dialogComments.getjCmbComments().addItem(it.next());
        }

        AutoCompleteDecorator.decorate(dialogComments.getjCmbComments());
        this.form = dialogComments;
    }

    @Override
    protected JDialog createForm(Frame parent) {
        return new JDialogComments(parent, this);
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected void loadReportOnForm(Report report) {
        JDialogComments dialogComments = (JDialogComments) this.form;
        dialogComments.getjTxtComments().setText(report.getComments() != null ? report.getComments() : "");
    }

    @Override
    protected void loadReportFromForm() {
        JDialogComments dialogComments = (JDialogComments) this.form;
        this.reportModel.updateComments(dialogComments.getjTxtComments().getText());
    }

    public void search() {
        try {
            JDialogComments dialogComments = (JDialogComments) this.form;
            ControllerCommentView controllerCommentView = new ControllerCommentView();
            controllerCommentView.showFormToSelect(null);
            if (controllerCommentView.getSelectedEntity() != null) {
                dialogComments.getjCmbComments().setSelectedItem(controllerCommentView.getSelectedEntity());
            }
        } catch (ExceptionDAO ex) {
            ErrorDialog.showError(this.form, ex.getMessage());
            this.facadeDB.refreshSession();
        }
    }

    public void addComment() {
        JDialogComments dialogComments = (JDialogComments) this.form;
        if ((dialogComments.getjCmbComments().getSelectedItem() != null) && (dialogComments.getjCmbComments().getSelectedIndex() >= 0)) {
            Comment comment = (Comment) dialogComments.getjCmbComments().getSelectedItem();
            dialogComments.getjTxtComments().append(comment.getText());
        }
    }

    public void newComment() {
        ControllerCommentEdition controllerCommentEdition = new ControllerCommentEdition();
        controllerCommentEdition.newEntity();
        JDialogComments dialogComments = (JDialogComments) this.form;
        try {
            List<Comment> comments = this.facadeDB.getCommentBroker().getAllComments();
            Iterator<Comment> it = comments.iterator();
            dialogComments.getjCmbComments().removeAllItems();
            while (it.hasNext()) {
                dialogComments.getjCmbComments().addItem(it.next());
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            ErrorDialog.showError(this.form, ex.getMessage());
        }
    }
}
